package com.ody.util.code.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.odianyun.util.exception.ExceptionUtils;
import com.odianyun.util.value.ValueUtils;
import org.springframework.util.StringUtils;

public class FieldBean implements Cloneable {
	private String name;
	private String title;
	private String column;
	private Class<?> type;
	private String typeFullName;
	private String typeName;
	private boolean primaryKey;
	private boolean nullable;
	private boolean common;
	private int size;
    private Integer min;
    private Integer max;
    private Integer precision;
    
    private String tag;
    private Integer tagSize;
    private String tagAttributes;
	
    private String dataSourceType;
    private String dataSourceKey;
    
    private String table;
    private String modelName;
    private String relName;
    
    public FieldBean() {}
	
	public FieldBean(String name, String column, String typeFullName, String title) {
		this.name = name;
		this.column = column;
		setTypeFullName(typeFullName);
		this.title = title;
	}
	
	public String getName() {
		return name;
	}
    public void setName(String name) {
        this.name = name;
    }
	public String getCapitalizedName() {
		return StringUtils.capitalize(name);
	}
    public String getCapName() {
        return StringUtils.capitalize(name);
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getColumn() {
		return column;
	}
	public void setColumn(String column) {
		this.column = column;
	}
	public Class<?> getType() {
		return type;
	}
	public String getTypeFullName() {
		return typeFullName;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeFullName(String typeFullName) {
		this.typeName = this.typeFullName = typeFullName;
		String className = typeFullName;
		
		int idx = typeFullName.lastIndexOf(".");
		if (idx != -1) {
			this.typeName =  typeFullName.substring(idx + 1);
		} else {
			className = "java.lang."+typeFullName;
		}
		try {
			type = Class.forName(className);
		} catch (Exception e) {
			throw ExceptionUtils.wrap2Runtime(e);
		}
	}
	public boolean isRequireTypeImport() {
		int idx = typeFullName.lastIndexOf(".");
		if (idx != -1) {
			return ! "java.lang".equals(typeFullName.substring(idx));
		}
		return false;
	}
	public boolean isPrimaryKey() {
		return primaryKey;
	}
	public void setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
	}
	public boolean isNullable() {
		return nullable;
	}
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}
    public boolean isCommon() {
        return common;
    }
    public void setCommon(boolean common) {
        this.common = common;
    }
    public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
    public Integer getMin() {
        return min;
    }
    public void setMin(Integer min) {
        this.min = min;
    }
    public Integer getMax() {
        return max;
    }
    public void setMax(Integer max) {
        this.max = max;
    }
    public Integer getPrecision() {
        return precision;
    }
    public void setPrecision(Integer precision) {
        this.precision = precision;
    }
    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }
    public Integer getTagSize() {
        return tagSize;
    }
    public void setTagSize(Integer tagSize) {
        this.tagSize = tagSize;
    }
    public String getTagAttributes() {
        return tagAttributes;
    }
    public void setTagAttributes(String tagAttributes) {
        this.tagAttributes = tagAttributes;
    }
    public String getDataSourceType() {
        return dataSourceType;
    }
    public void setDataSourceType(String dataSourceType) {
        this.dataSourceType = dataSourceType;
    }
    public String getDataSourceKey() {
        return dataSourceKey;
    }
    public void setDataSourceKey(String dataSourceKey) {
        this.dataSourceKey = dataSourceKey;
    }
    
    public String getTable() {
        return table;
    }
    public void setTable(String table) {
        this.table = table;
    }
    public String getModelName() {
        return modelName;
    }
    public void setModelName(String modelName) {
        this.modelName = modelName;
    }
    public String getRelName() {
        return ValueUtils.ifNull(relName, name);
    }
    public void setRelName(String relName) {
        this.relName = relName;
    }
    
    public boolean isNumber() {
        return Number.class.isAssignableFrom(type);
    }
    public boolean isString() {
        return "String".equals(typeName);
    }
    public boolean isText() {
        return isString() && getSize() > 500;
    }
    public boolean isDate() {
        return "Date".equals(typeName);
    }
    public boolean isRequired() {
        return !nullable;
    }
    public void setRequired(boolean required) {
        this.nullable = !required;
    }
    
    public String getTagAttrs() {
        if (StringUtils.hasText(tagAttributes)) {
            JSONObject json = JSON.parseObject(tagAttributes);
            StringBuilder buff = new StringBuilder();
            for (String key : json.keySet()) {
                buff.append(" ").append(key).append("=\"").append(json.getString(key)).append("\"");
            }
            return buff.toString();
        }
        return "";
    }
    
    @Override
	public String toString() {
		return "name:" + name + ",title:" + title + ", pk:" + primaryKey + ", nullable:" + nullable;
	}
    
    @Override
    protected FieldBean clone()  {
        try {
            return (FieldBean) super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}
